package com.srwl.mytx.widget;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.adapter.DialogAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dahai2070 on 2017/2/19 0019.
 */

@Deprecated
public class BottomDialogOld extends DialogFragment {


    private static final String DIALOG_ITEMS_KEY = "dialogItems";
    public static final int AREA_FLAG_UP = 2;
    public static final int AREA_FLAG_CENTER = 1;
    public static final int AREA_FLAG_BOTTOM = 0;
    private static final String AREA_FLAG_KEY = "areaFlag";
    private int areaFlag = 0;

    // @BindView(R.id.rv_dialog)
    RecyclerView rvDialogItems;
    DialogAdapter dialogAdapter;
    private OnItemClickListener mListener;
    private List<DialogItem> dialogItems;

    public BottomDialogOld() {
    }

    public static BottomDialogOld newInstance(ArrayList<DialogItem> dialogItems, int areaFlag) {

        Bundle args = new Bundle();
        args.putParcelableArrayList(DIALOG_ITEMS_KEY, dialogItems);
        args.putInt(AREA_FLAG_KEY, areaFlag);
        //args.putSerializable(DIALOG_ITEMS_KEY,dialogItems);
        BottomDialogOld fragment = new BottomDialogOld();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.bottom_dialog, container, false);
        // ButterKnife.bind(this, ll_buttonView);
        rvDialogItems = view.findViewById(R.id.rv_dialog);
        dialogItems = getArguments().getParcelableArrayList(DIALOG_ITEMS_KEY);
        areaFlag = getArguments().getInt(AREA_FLAG_KEY);
        dialogAdapter = new DialogAdapter(getContext(), dialogItems);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
        rvDialogItems.setLayoutManager(gridLayoutManager);
        rvDialogItems.setAdapter(dialogAdapter);
        dialogAdapter.setOnItemClickListener(new DialogAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                dismiss();
                if (position != dialogItems.size()) {
                    mListener.onClick(position, dialogItems.get(position).getDialogItemName());
                }
            }
        });

        return view;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(),R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
        dialog.setContentView(R.layout.bottom_dialog);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        dialog.setCanceledOnTouchOutside(true);

        // 设置宽度为屏宽、在屏幕中的位置
        Window window = dialog.getWindow();
        // window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams wlp = window.getAttributes();
        switch (areaFlag) {
            case AREA_FLAG_UP:
                wlp.gravity = Gravity.TOP;
                break;
            case AREA_FLAG_CENTER:
                wlp.gravity = Gravity.CENTER;
                break;
            default:
                wlp.gravity = Gravity.BOTTOM;
        }
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
        return dialog;
    }

    public interface OnItemClickListener {
        void onClick(int position, String itemType);

        void onDismiss();

    }

    public void setOnItemClickListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mListener != null) {
            mListener.onDismiss();
        }
    }

//    @Override
//    public void onStart() {
//        super.onStart();
//
//        Window window = getDialog().getWindow();
//        ColorDrawable dw = new ColorDrawable(0xbffffff);
//        window.setBackgroundDrawable(dw);
//        WindowManager.LayoutParams windowParams = window.getAttributes();
//        windowParams.dimAmount = 0.0f;//设置弹出对话框后，对话框以外的黑色背景的透明度
//        window.setAttributes(windowParams);
//    }
}

